﻿var DataDict = {
    // 级别
    data_ORGANIZATION_LVLType: [{ value: "1", text: lang.oil },
                                { value: "2", text: lang.unit },
                                { value: "3", text: lang.office },
                                { value: "4", text: lang.department}]
                        };
$.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj); 
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidPARENTID').combotree({ multiple: false });
                $('#formidPARENTID').combotree('loadData', _dataarr);
                if (_currentval != '') {
                    $('#formidPARENTID').combotree('setValue', _currentval);
                }

            }

        }
    });






var organizationedit = {
    formDefine: {
        labelWidth: 80, inputWidth: 150, space: 10, labelAlign: 'left',
        fields: [
                    { display: lang.Department_ID +"：", name: "ORGANIZATION_CODE", type: "text", newline: true, inputWidth: 240, width: 340, serviceDataName: 'ORGANIZATION_CODE' },
                    { display:lang.Department_name+"：", name: "ORGANIZATION_NAME", type: "text", inputWidth: 240, newline: true, width: 340, serviceDataName: 'ORGANIZATION_NAME' },
                    { display: lang.head+"：", name: "ORGANIZATION_MANAGER", type: "text", newline: true, inputWidth: 240, width: 340, serviceDataName: 'ORGANIZATION_MANAGER' },
                    { display: lang.Unit_level+"：", name: "ORGANIZATION_LVL", type: "select", newline: true, inputWidth: 240, data: 'data_ORGANIZATION_LVLType', serviceDataName: 'ORGANIZATION_LVL' },
                    { display: lang.Department_F + "：", name: "PARENTID", type: "selecttree", newline: true, inputWidth: 240, dataoptions: "panelHeight:150" },
                    { display: lang.sequence+"：", name: "SORTCODE", type: "text", inputWidth: 240, newline: true, serviceDataName: 'SORTCODE' },
                    { display: lang.Remarks+"：", name: "ORGANIZATION_REMARK", type: "textarea", newline: true, inputWidth: 240, width: 340, serviceDataName: 'ORGANIZATION_REMARK' },
                    { display: lang.save, name: "search", type: "button", newline: true, nlstyle: "text-align:center", width: 280, click: function () { organizationedit.form.submit(); } },
                    { display: lang.close, name: "close", type: "button", width: 280, click: function () {
                        parent.$.closePage();
                    }
                    }
                ]
                ,
        onLoaded: function (data, form) {
            var _currentval = '';
            var _currentval1 = '';
            if (data && data.PARENTID != '') {
                _currentval = data.PARENTID;
            }
            if (data && data.ITEMVALUE != '') {
                _currentval1 = data.ITEMNAME;
            }
            loadOrgData(_currentval);
            //loadLVLData(_currentval1);
        }
                ,
        submitUrl: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        submitUrlType: "EditOrganizationByID",
        submitCheck: function (params) {

            if (!params["ORGANIZATION_CODE"] || params["ORGANIZATION_CODE"] == "") {
                return lang.Department_ID_cannot_be_empty;
            }
            if (!params["ORGANIZATION_NAME"] || params["ORGANIZATION_NAME"] == "") {
                return lang.Department_name_cannot_be_empty;
            }
            if (!params["PARENTID"] || params["PARENTID"] == "") {
                return lang.P_Department_name_cannot_be_empty;
            }
            if (!params["SORTCODE"] || params["SORTCODE"] == "") {
                return lang.sequence_cannot_be_empty;
            }
            if (isNaN(params["SORTCODE"])) {
                return lang.sequence_must_be_digital;
            }

            return "";
        },
        submitParams: function (params) {
            if ($.getPara('orgid') && $.getPara('orgid') != '') {
                params.action = 1;
                params.ORGANIZATION_ID = $.getPara('orgid');
            } else { params.action = 2; }

            return params;
        },
        submitSuccess: function () {
            if ($.getPara('orgid') && $.getPara('orgid') != '') {
                $.alert(lang.modify_new_department_success, null, function () { parent.organizationlist.search(); parent.$.closePage(); });
            } else {
                $.alert(lang.add_new_department_success, null, function () { parent.organizationlist.search(); parent.$.closePage(); });
            }
        }
    },
    init: function () {
        if ($.getPara('orgid') != '') {
            this.formDefine.url = App_Config.TadongUrl + "/AjaxMappingHandler.aspx";
            this.formDefine.urlType = "GetOrganizeInfoByID";
            this.formDefine.params = function () { return $.getPara('orgid'); };
        }
        jsPageRender(this);
    }
};


//#region 加载组织数据
function loadOrgData(_currentval) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryOrganizeList",
        success: function (data) {
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ORGANIZATION_ID;

                        obj.text = data[i].ORGANIZATION_NAME;
                        getsubOrg(data, data[i].ORGANIZATION_ID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidPARENTID').combotree({ multiple: false });
                $('#formidPARENTID').combotree('loadData', _dataarr);
                if (_currentval != '') {
                    $('#formidPARENTID').combotree('setValue', _currentval);
                }

            }

        }
    });

    function getsubOrg(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ORGANIZATION_ID;
                    obj.text = _d[i].ORGANIZATION_NAME;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubOrg(_d, _sdata[j].id, _sdata[j]);
                }
            }
        }
    }
}
    

    //#region 加载单位级别
function loadLVLData(_currentval1) {

    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryDictItemList2",
        model: function () { return 1001; },
        success: function (data) {
            //alert("成功");
            if (data && data.length > 0) {
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    var obj = {};
                    obj.id = data[i].ITEMVALUE;
                    obj.text = data[i].ITEMNAME;
                    alert(obj.id);
                    alert(obj.text);
                    _data.push(obj);
                }
                $("#formidITEMNAME").datebox("setValue", _currentval1);

                //                rootobj.children = _data;
                //                _dataarr.push(rootobj);
                //                $('#formidITEMNAME').combotree({ multiple: false });
                //                $('#formidITEMNAME').combotree('loadData', _dataarr);
                //                if (_currentval1 != '') {
                //                    $('#formidITEMNAME').combotree('setValue', _currentval1);
                //                }

            }
        }
    });   
}